package com.func.domain.strategy.model.entity;

import com.func.types.common.Constants;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.lang3.StringUtils;

/**
 * @ClassName StrategyEntity
 * @Description 策略实体类, 主要添加了从RuleModels字段中检查是否有rule_weight内容的函数
 * @Author: func
 * @Date 2025/2/21 21:23
 * @Version 1.0
 */

@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class StrategyEntity {

    /**抽奖策略ID */
    private Long strategyId;
    /**抽奖策略描述 */
    private String strategyDesc;
    /**策略模型 ,需要做字符串解析*/
    private String ruleModels;

    /**
     * 将入了Models字段的内容根据分隔符 ',', 分割成多个String
     * @return 分割以后的String数组, 如果这个字段的内容是空, 则返回null
     */
    public String[] ruleModels(){
        if (StringUtils.isBlank(this.ruleModels) || ruleModels == null) return null;
        return ruleModels.split(Constants.SPLIT);
    }

    /**
     * 从RuleModels中检查是否有rule_weight
     * @return 如果有这个规则, 则返回rule_weight, 否则null
     */
    public String getRuleWeight() {
        String[] ruleModels = this.ruleModels();
        if (ruleModels == null) return null;
        for (String ruleModel : ruleModels) {
            if ("rule_weight".equals(ruleModel)) return ruleModel;
        }
        return null;
    }

}
